package com.donn.securitycontrolplatform.common;

import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.icc.multiinone.accesscontrol.AccessRecordSynSDK;
import com.dahuatech.icc.multiinone.event.domain.BaseEventSubcribeInfo;
import com.dahuatech.icc.multiinone.face.FaceRecordSynSDK;
import com.dahuatech.icc.multiinone.vo.BaseResponse;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigUserPwdInfo;
import com.donn.securitycontrolplatform.utils.DaHuaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @ClassName Initialize
 * @Author Glenn
 * @Date 2021/11/8 21:30
 * @Description 项目相关资源初始化
 * @Version 1.0
 */
//该方法在项目启动后 Application.run之前调用 用于初始化相关资源@Order 是多个初始化类之间的加载顺序 数字越小优先级越高
@Order(1)
@Component
@Slf4j
public class Initialize implements CommandLineRunner {
    public static final String LOCAL_HOST_ADDRESS = "192.168.253.201";
    private String https = "443";//https 默认端口443，做了内外网映射 20033
    private String http = "83";//http http调试模式端口默认是83，未作外网映射，此环境不支持83端口调试

    //执行操作
    @Override
    public void run(String... args) throws Exception {
        //初始化大华消息订阅
        log.info("开始订阅人脸布控消息...");
        initDahuaFaceMsg();
        log.info("开始订阅门禁消息...");
        initDahuaMjMsg();
    }

    public void initDahuaMjMsg() {
        OauthConfigUserPwdInfo oauthConfigBaseInfo = new OauthConfigUserPwdInfo(DaHuaUtil.HOST, DaHuaUtil.CLIENT_ID, DaHuaUtil.CLIENT_SECRET, DaHuaUtil.USER_NAME, DaHuaUtil.PASSWORD, false, https, http);
        AccessRecordSynSDK pushPersonCardToThirdPartySDK = new AccessRecordSynSDK();
        BaseEventSubcribeInfo pushPersonCardToThirdPartyRequest = new BaseEventSubcribeInfo();
        pushPersonCardToThirdPartyRequest.setHttps(false);
        pushPersonCardToThirdPartyRequest.setName("门禁消息订阅");
        pushPersonCardToThirdPartyRequest.setReceiveIp(LOCAL_HOST_ADDRESS);
        pushPersonCardToThirdPartyRequest.setReceivePort("8088");
        pushPersonCardToThirdPartyRequest.setUri("/scp/api/common/mj/msg");
        pushPersonCardToThirdPartyRequest.setOauthConfigBaseInfo(oauthConfigBaseInfo);
        BaseResponse baseResponse = pushPersonCardToThirdPartySDK.eventSubscribe(pushPersonCardToThirdPartyRequest);
        log.info("门禁消息订阅完成...");

    }

    public void initDahuaFaceMsg() {
        OauthConfigUserPwdInfo oauthConfigBaseInfo = new OauthConfigUserPwdInfo(DaHuaUtil.HOST, DaHuaUtil.CLIENT_ID, DaHuaUtil.CLIENT_SECRET, DaHuaUtil.USER_NAME, DaHuaUtil.PASSWORD, false, https, http);
        FaceRecordSynSDK pushPersonCardToThirdPartySDK = new FaceRecordSynSDK();
        BaseEventSubcribeInfo pushPersonCardToThirdPartyRequest = new BaseEventSubcribeInfo();
        pushPersonCardToThirdPartyRequest.setHttps(false);
        pushPersonCardToThirdPartyRequest.setName("人脸布控消息订阅");
        pushPersonCardToThirdPartyRequest.setReceiveIp(LOCAL_HOST_ADDRESS);
        pushPersonCardToThirdPartyRequest.setReceivePort("8088");
        pushPersonCardToThirdPartyRequest.setUri("/scp/api/common/face/msg");
        pushPersonCardToThirdPartyRequest.setOauthConfigBaseInfo(oauthConfigBaseInfo);
        BaseResponse baseResponse = pushPersonCardToThirdPartySDK.eventSubscribe(pushPersonCardToThirdPartyRequest);
        log.info("人脸布控消息订阅完成...");

    }

}
